package com.huawei.secure.android.common.ssl;

import android.text.TextUtils;
import android.util.Log;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

public class h {
    private static final String TAG = "ZipUtil";
    private static final int af = 104857600;
    private static final int ag = 100;
    private static final int ah = 4096;
    private static final String ai = "../";

    public static boolean b(String str, String str2) throws e {
        return a(str, str2, 104857600, 100);
    }

    public static boolean a(String str, String str2, int i, int i2) throws e {
        FileInputStream fileInputStream;
        ZipInputStream zipInputStream;
        IOException e;
        OutputStream outputStream;
        BufferedOutputStream bufferedOutputStream;
        Throwable th;
        Log.i(TAG, "unZip: ");
        List arrayList = new ArrayList();
        if (!b(str, str2, i, i2)) {
            return false;
        }
        if (str2.endsWith(File.separator) && str2.length() > File.separator.length()) {
            str2 = str2.substring(0, str2.length() - File.separator.length());
        }
        boolean z = true;
        byte[] bArr = new byte[ah];
        BufferedOutputStream bufferedOutputStream2 = null;
        FileInputStream fileInputStream2 = null;
        ZipInputStream zipInputStream2 = null;
        FileOutputStream fileOutputStream = null;
        try {
            fileInputStream = new FileInputStream(str);
            try {
                zipInputStream = new ZipInputStream(new BufferedInputStream(fileInputStream));
                while (true) {
                    try {
                        ZipEntry nextEntry = zipInputStream.getNextEntry();
                        if (nextEntry == null) {
                            break;
                        }
                        BufferedOutputStream bufferedOutputStream3;
                        File file = new File(str2 + File.separator + nextEntry.getName());
                        if (nextEntry.isDirectory()) {
                            d(file);
                            arrayList.add(file);
                            bufferedOutputStream3 = bufferedOutputStream2;
                        } else {
                            File parentFile = file.getParentFile();
                            if (!(parentFile == null || parentFile.exists())) {
                                d(parentFile);
                            }
                            OutputStream fileOutputStream2 = new FileOutputStream(file);
                            OutputStream bufferedOutputStream4;
                            try {
                                bufferedOutputStream4 = new BufferedOutputStream(fileOutputStream2);
                                int i3 = 0;
                                while (i3 + ah <= i) {
                                    try {
                                        int read = zipInputStream.read(bArr, 0, ah);
                                        if (read == -1) {
                                            break;
                                        }
                                        bufferedOutputStream4.write(bArr, 0, read);
                                        i3 += read;
                                    } catch (IOException e2) {
                                        e = e2;
                                        outputStream = fileOutputStream2;
                                        zipInputStream2 = zipInputStream;
                                        fileInputStream2 = fileInputStream;
                                        bufferedOutputStream = bufferedOutputStream4;
                                        fileOutputStream = outputStream;
                                    } catch (Throwable th2) {
                                        th = th2;
                                        OutputStream outputStream2 = bufferedOutputStream4;
                                        bufferedOutputStream4 = fileOutputStream2;
                                    }
                                }
                                File file2 = new File(str2, c.x);
                                if (file.getName().endsWith(".bks")) {
                                    file.renameTo(file2);
                                    arrayList.add(file2);
                                } else {
                                    arrayList.add(file);
                                }
                                bufferedOutputStream4.flush();
                                d.a(bufferedOutputStream4);
                                d.a(fileOutputStream2);
                                outputStream = fileOutputStream2;
                                fileOutputStream2 = bufferedOutputStream4;
                                bufferedOutputStream4 = outputStream;
                            } catch (IOException e3) {
                                e = e3;
                                bufferedOutputStream4 = fileOutputStream2;
                                zipInputStream2 = zipInputStream;
                                fileInputStream2 = fileInputStream;
                                bufferedOutputStream = bufferedOutputStream2;
                            } catch (Throwable th3) {
                                th = th3;
                                bufferedOutputStream4 = fileOutputStream2;
                            }
                        }
                        try {
                            zipInputStream.closeEntry();
                            bufferedOutputStream2 = bufferedOutputStream3;
                        } catch (IOException e4) {
                            e = e4;
                            ZipInputStream zipInputStream3 = zipInputStream;
                            fileInputStream2 = fileInputStream;
                            bufferedOutputStream = bufferedOutputStream3;
                            zipInputStream2 = zipInputStream3;
                        } catch (Throwable th4) {
                            th = th4;
                            bufferedOutputStream2 = bufferedOutputStream3;
                        }
                    } catch (IOException e5) {
                        e = e5;
                        zipInputStream2 = zipInputStream;
                        fileInputStream2 = fileInputStream;
                        bufferedOutputStream = bufferedOutputStream2;
                    } catch (Throwable th5) {
                        th = th5;
                    }
                }
                d.a(zipInputStream);
                d.a(fileInputStream);
                a(fileInputStream, bufferedOutputStream2, zipInputStream, fileOutputStream);
            } catch (IOException e6) {
                e = e6;
                fileInputStream2 = fileInputStream;
                bufferedOutputStream = null;
                try {
                    Log.e(TAG, "Unzip IOException : " + e.getMessage());
                    a(fileInputStream2, bufferedOutputStream, zipInputStream2, fileOutputStream);
                    z = false;
                    if (!z) {
                        return z;
                    }
                    a(arrayList);
                    return z;
                } catch (Throwable th6) {
                    th = th6;
                    bufferedOutputStream2 = bufferedOutputStream;
                    fileInputStream = fileInputStream2;
                    zipInputStream = zipInputStream2;
                    a(fileInputStream, bufferedOutputStream2, zipInputStream, fileOutputStream);
                    throw th;
                }
            } catch (Throwable th7) {
                th = th7;
                zipInputStream = null;
                a(fileInputStream, bufferedOutputStream2, zipInputStream, fileOutputStream);
                throw th;
            }
        } catch (IOException e7) {
            e = e7;
            bufferedOutputStream = null;
            Log.e(TAG, "Unzip IOException : " + e.getMessage());
            a(fileInputStream2, bufferedOutputStream, zipInputStream2, fileOutputStream);
            z = false;
            if (!z) {
                return z;
            }
            a(arrayList);
            return z;
        } catch (Throwable th8) {
            th = th8;
            fileInputStream = null;
            zipInputStream = null;
            a(fileInputStream, bufferedOutputStream2, zipInputStream, fileOutputStream);
            throw th;
        }
        if (!z) {
            return z;
        }
        a(arrayList);
        return z;
    }

    private static boolean a(String str, int i, int i2) {
        IOException e;
        Throwable th;
        long j = 0;
        ZipFile zipFile;
        try {
            zipFile = new ZipFile(str);
            try {
                boolean z;
                Enumeration entries = zipFile.entries();
                int i3 = 0;
                while (entries.hasMoreElements()) {
                    ZipEntry zipEntry = (ZipEntry) entries.nextElement();
                    j += zipEntry.getSize();
                    i3++;
                    if (!zipEntry.getName().contains(ai) && i3 < i2 && j <= ((long) i)) {
                        if (zipEntry.getSize() == -1) {
                            Log.e(TAG, "File name is invalid or too many files or too big");
                            z = false;
                            break;
                        }
                    }
                    Log.e(TAG, "File name is invalid or too many files or too big");
                    z = false;
                    break;
                }
                z = true;
                if (zipFile == null) {
                    return z;
                }
                try {
                    zipFile.close();
                    return z;
                } catch (IOException e2) {
                    Log.e(TAG, "close zipFile IOException ");
                    return z;
                }
            } catch (IOException e3) {
                e = e3;
                try {
                    Log.e(TAG, "not a valid zip file, IOException : " + e.getMessage());
                    if (zipFile != null) {
                        return false;
                    }
                    try {
                        zipFile.close();
                        return false;
                    } catch (IOException e4) {
                        Log.e(TAG, "close zipFile IOException ");
                        return false;
                    }
                } catch (Throwable th2) {
                    th = th2;
                    if (zipFile != null) {
                        try {
                            zipFile.close();
                        } catch (IOException e5) {
                            Log.e(TAG, "close zipFile IOException ");
                        }
                    }
                    throw th;
                }
            }
        } catch (IOException e6) {
            e = e6;
            zipFile = null;
            Log.e(TAG, "not a valid zip file, IOException : " + e.getMessage());
            if (zipFile != null) {
                return false;
            }
            zipFile.close();
            return false;
        } catch (Throwable th3) {
            th = th3;
            zipFile = null;
            if (zipFile != null) {
                zipFile.close();
            }
            throw th;
        }
    }

    private static void b(File file) {
        if (file != null) {
            if (file.isFile()) {
                c(file);
            } else if (file.isDirectory()) {
                File[] listFiles = file.listFiles();
                if (listFiles == null || listFiles.length == 0) {
                    c(file);
                    return;
                }
                for (File b : listFiles) {
                    b(b);
                }
                c(file);
            }
        }
    }

    private static boolean b(String str, String str2, int i, int i2) throws e {
        if (TextUtils.isEmpty(str) || str.contains(ai)) {
            Log.e(TAG, "zip file is not valid");
            return false;
        } else if (TextUtils.isEmpty(str2) || str2.contains(ai)) {
            Log.e(TAG, "target directory is not valid");
            return false;
        } else if (a(str, i, i2)) {
            return true;
        } else {
            Log.e(TAG, "zip file contains valid chars or too many files");
            throw new e("unsecure zipfile!");
        }
    }

    private static boolean a(List<File> list) {
        try {
            for (File b : list) {
                b(b);
            }
            return true;
        } catch (Exception e) {
            Log.e(TAG, "unzip fail delete file failed" + e.getMessage());
            return false;
        }
    }

    private static void a(FileInputStream fileInputStream, BufferedOutputStream bufferedOutputStream, ZipInputStream zipInputStream, FileOutputStream fileOutputStream) {
        d.a(fileInputStream);
        d.a(bufferedOutputStream);
        d.a(zipInputStream);
        d.a(fileOutputStream);
    }

    private static void c(File file) {
        if (file != null && !file.delete()) {
            Log.e(TAG, "delete file error");
        }
    }

    private static void d(File file) {
        if (file != null && !file.exists() && !file.mkdirs()) {
            Log.e(TAG, "mkdirs error , files exists or IOException.");
        }
    }
}
